<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.net.IframeLoadMonitor</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.events');
    goog.require('goog.net.IframeLoadMonitor');
    goog.require('goog.testing.AsyncTestCase');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
<div id="frame_parent"></div>
<script>
  var TEST_FRAME_SRCS = ['iframeloadmonitor_test_frame.html',
      'iframeloadmonitor_test_frame2.html',
      'iframeloadmonitor_test_frame3.html'];

  // Create a new test case.
  var iframeLoaderTestCase = new goog.testing.AsyncTestCase(document.title);
  iframeLoaderTestCase.stepTimeout = 4 * 1000;

  // Array holding all iframe load monitors.
  iframeLoaderTestCase.iframeLoadMonitors_ = [];

  // How many single frames finished loading
  iframeLoaderTestCase.singleComplete_ = 0;

  /** Sets up the test environment, adds tests and sets up the worker pools. */
  iframeLoaderTestCase.setUpPage = function() {
    this.log('Setting tests up');
    iframeLoaderTestCase.waitForAsync('loading iframes');

    var dom = goog.dom.getDomHelper();

    // Load single frame
    var frame = dom.createDom('iframe');
    this.iframeLoadMonitors_.push(new goog.net.IframeLoadMonitor(frame));
    goog.events.listen(this.iframeLoadMonitors_[0],
        goog.net.IframeLoadMonitor.LOAD_EVENT, this);
    var frameParent = dom.getElement('frame_parent');
    dom.appendChild(frameParent, frame);
    this.log('Loading frame at: ' + TEST_FRAME_SRCS[0]);
    frame.src = TEST_FRAME_SRCS[0];

    // Load single frame with content check
    var frame1 = dom.createDom('iframe');
    this.iframeLoadMonitors_.push(new goog.net.IframeLoadMonitor(frame1, true));
    goog.events.listen(this.iframeLoadMonitors_[1],
        goog.net.IframeLoadMonitor.LOAD_EVENT, this);
    var frameParent = dom.getElement('frame_parent');
    dom.appendChild(frameParent, frame1);
    this.log('Loading frame with content check at: ' + TEST_FRAME_SRCS[0]);
    frame1.src = TEST_FRAME_SRCS[0];
  };

  /** Handles any events fired */
  iframeLoaderTestCase.handleEvent = function(e) {
    this.log('handleEvent, type: ' + e.type);
    if (e.type == goog.net.IframeLoadMonitor.LOAD_EVENT) {
      this.singleComplete_++;
      this.callbacksComplete();
    }
  };

  /** Checks if all the load callbacks are done*/
  iframeLoaderTestCase.callbacksComplete = function() {
    if (this.singleComplete_ == 2) {
      iframeLoaderTestCase.continueTesting();
    }
  }

  /** Tests the results. */
  iframeLoaderTestCase.addNewTest('testResults', function() {
    this.log('getting test results');
    for (var i = 0; i < this.iframeLoadMonitors_.length; i++) {
      assertTrue(this.iframeLoadMonitors_[i].isLoaded());
    }
  });

  /** Standalone Closure Test Runner. */
  G_testRunner.initialize(iframeLoaderTestCase);

</script>
</body>
</html>
